<template>
  <div>
    <!--<el-form label-width="200px" label-position="left">
        <el-form-item label="评审组员">-->
    <el-table :data="tableData2" row-key="id" highlight-current-row border>
      <el-table-column prop="realName" :label="whatcheck==2?'岗位名称':'姓名'" align="center">
        <template slot-scope="{ row }">
          <span v-if="whatcheck==1" style="color:#666">{{ row.realName }}</span>
          <span v-else style="color:#666">{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="whatcheck!==2" align="center" prop="name" label="手机号" />
      <el-table-column prop="iorder" align="center" label="评审顺序" sortable>
        <template slot-scope="{ row }">
          <span style="color:#666">{{ row.iorder }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="checkResult" label="评审状态">
        <template slot-scope="{ row }">
          <span v-if="row.checkResult==0&&row.noCheck!==1" style="color:#666">结果未出</span>
          <span v-if="row.checkResult==1&&row.noCheck!==1" style="color:#00e600">通过</span>
          <span v-if="row.checkResult==2&&row.noCheck!==1" style="color:red">驳回</span>
          <span v-if="row.noCheck==1">抄送</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!--</el-form-item>

      </el-form>-->

  </div>
</template>
<script>

import waves from '@/directive/waves' // waves directive
import { getPerson, list, check, getcheck, updateList } from '@/api/newSupplierApprove'
import { getInfo, add, update, deleteInfo, supplierChange, download, getfile, getinfor, companyList, getfileOnSupplier, getSupplierCategory } from '@/api/supplier'
import { getorgByUsername } from '@/api/user'
import { getwhartCheck } from '@/api/approvalProcessSettings'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { deepClone, arraryRemove, parseTime1, downfile, downLoadTemplate } from '@/utils'
import Menu from '@/configData/configMenu'
const project = {
  id: 0,
  supplierCode: '',
  supplierName: '',
  address: '',
  contact: '',
  mobilePhone: '',
  email: '',
  type: '',
  estimateSpend: '',
  companyId: ''
}
const projectVo = {
  leaderId: 0,
  membersId: 0,
  supplierId: 0,
  leaderWeight: 0,
  memberWeight: 0
}

export default {
  name: 'Project',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      fullscreenLoading: false,
      fileSupplider2: '',
      fileSupplider1: '',
      project1: {},
      suplierType: [
        {
          val: 1,
          txt: '直接物料供应商'
        },
        {
          val: 2,
          txt: '间接物料供应商'
        }, {
          val: 3,
          txt: '外协供应商'
        }],
      checked: [],
      companyListData: [],
      addressTypeList: [
        {
          'txt': '天津',
          'val': 1
        }, {
          'txt': '苏州',
          'val': 2
        },
        {
          'txt': '国内其他',
          'val': 3
        }, {
          'txt': '国外',
          'val': 4
        }],
      showtxt: true,
      pagesize: 5,
      currentPage: 1,
      loading: false,
      getShenPi: '',
      endTime4: '',
      endTime5: '',
      endTime6: '',
      endTime7: '',
      iscan: [{
        text: '是',
        val: 1

      },
      {
        text: '否',
        val: 2

      }],
      categoryListLoading: false,
      file1: {},
      file2: {},
      file3: {},
      file4: {},
      file5: {},
      file6: {},
      file7: {},
      ishave: [{
        text: '是',
        val: 1

      },
      {
        text: '否',
        val: 2

      }],
      ishaveZheng: [{
        text: '是',
        val: 1

      },
      {
        text: '否',
        val: 2

      }],
      typeList: [
        {
          text: '工厂',
          val: 1

        },
        {
          text: '代理',
          val: 2

        }

      ],
      stateShenPi: [
        {
          'txt': '不可用',
          'val': '-1'
        }, {
          'txt': '待认证',
          'val': '1'
        },
        {
          'txt': '待审核',
          'val': '2'
        }, {
          'txt': '正式',
          'val': '3'
        }],
      whatcheck: 1,
      txtName: '',
      tablelog: [],
      showpan: false,
      shenpiId: '',
      urlMenu: Menu.newSupplierApprove,
      tableData2: [],
      text: '',
      dialogType: 'new',
      query: {
        checkStatus: 1,
        supplierName: '',
        pageIndex: 1,
        pageSize: 20
      },
      total: 0,
      dialogVisible: false,
      project: [],
      tableData: [],
      pageIndex: 1,
      fileSupplider2Data: {},
      fileSupplider1Data: {},
      categoryList: []
    }
  },
  created() {

  },
  mounted() {
    // this.handleUpdate()
    // this.newsupplierList()
  },
  methods: {

    getemail(row, column) {
      if (row.email && row.email.indexOf(',') !== -1) {
        const arr = row.email.split(',')
        return arr[0]
      } else {
        return row.email
      }
    },
    downloadExcel(nameOrigin, name) {
      downLoadTemplate(nameOrigin, name)
    },
    getSupplierCategoryFun() {
      const obj = {
        supplierId: this.project1.id

      }
      this.categoryListLoading = true
      getSupplierCategory(obj).then((res) => {
        this.categoryListLoading = false
        if (res.isSuccess) {
          this.categoryList = res.data
        } else {
          this.$message.error('添加失败')
        }
      }).catch(() => {
        this.categoryListLoading = false
      })
    },
    getfileOnSupplierFun(type) {
      const obj = {
        typeid: type
      }
      getfileOnSupplier(this.project1.id, obj).then((res) => {
        const file = res.data[0]
        if (file) {
          if (type == 11) {
            this.fileSupplider1Data = res.data[0]
            this.fileSupplider1 = res.data[0].fileName
          } else if (type == 12) {
            this.fileSupplider2Data = res.data[0]
            this.fileSupplider2 = res.data[0].fileName
          }
        }
      })
    },
    downloadfileOnSupplierFun(type) {
      let id = ''
      if (type == 11) {
        id = this.fileSupplider1Data.id
      } else if (type == 12) {
        id = this.fileSupplider2Data.id
      }
      download(id).then((res) => {
        if (type == 11) {
          downfile(this.fileSupplider1, res)
        } else if (type == 12) {
          downfile(this.fileSupplider2, res)
        }
      })
    },
    getcompanyList() {
      companyList().then((res) => {
        this.companyListData = res.data
      })
    },
    getData() {
      // let me = this;
      // 获取数据，在获取数据成功后，则关闭loading效果
      this.loading = false
    },
    getMData() {
      // let me = this;
      this.pagesize += 5
      if (this.pagesize >= this.tablelog.length) {
        this.showtxt = false
      }
      this.loading = true
      this.getData()
    },

    handleShowProjectFile1(type) {
      var row = {}
      if (type == 1) {
        row = this.file1
      } else if (type == 2) {
        row = this.file2
      } else if (type == 3) {
        row = this.file3
      } else if (type == 4) {
        row = this.file4
      } else if (type == 5) {
        row = this.file5
      } else if (type == 6) {
        row = this.file6
      } else if (type == 7) {
        row = this.file7
      }
      if (!row.id) {
        this.$message({
          type: 'warning',
          message: '此处还未上传文件'
        })
        return false
      }
      download(row.id).then(res => {
        const content = res
        const blob = new Blob([content]) // 构造一个blob对象来处理数据
        const fileName = row.fileName // 导出文件名
        if ('download' in document.createElement('a')) { // 支持a标签download的浏览器
          const link = document.createElement('a') // 创建a标签
          link.download = fileName // a标签添加属性
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          document.body.appendChild(link)
          link.click() // 执行下载
          URL.revokeObjectURL(link.href) // 释放url
          document.body.removeChild(link) // 释放标签
        } else { // 其他浏览器
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    getfiles(type) {
      const obj = {
        typeid: type
      }

      getfile(this.project.id, obj).then((res) => {
        const file = res.data[0]
        if (file) {
          if (type == 1) {
            this.file1 = res.data[0]
          } else if (type == 2) {
            this.file2 = res.data[0]
          } else if (type == 3) {
            this.file3 = res.data[0]
          } else if (type == 4) {
            this.file4 = res.data[0]
            if (this.file4.endTime) {
              this.endTime4 = parseTime1(this.file4.endTime)
            }
          } else if (type == 5) {
            this.file5 = res.data[0]
            if (this.file5.endTime) {
              this.endTime5 = parseTime1(this.file5.endTime)
            }
          } else if (type == 6) {
            this.file6 = res.data[0]
            if (this.file6.endTime) {
              this.endTime6 = parseTime1(this.file6.endTime)
            }
          } else if (type == 7) {
            this.file7 = res.data[0]
            if (this.file7.endTime) {
              this.endTime7 = parseTime1(this.file7.endTime)
            }
          }
        }
      })
    },
    updateListSuplier(row) {
      const obj = {
        supplierCertificateId: row.id
      }
      updateList(obj).then((res) => {
        if (res.data.data) {
          this.tablelog = res.data.data
          if (this.pagesize >= this.tablelog.length) {
            this.showtxt = false
          }
        }
      })
    },
    getInfor(row) {
      this.file1 = {}
      this.file2 = {}
      this.file3 = {}
      this.file4 = {}
      this.file5 = {}
      this.file6 = {}
      this.file7 = {}

      const obj = {
        supplierId: row.id
      }
      getinfor(obj).then((res) => {
        if (res.data.items[0]) {
          this.project = deepClone(res.data.items[0])
          console.log(this.project)
          if (this.project.email.indexOf(',') !== -1) {
            const arr = this.project.email.split(',')
            this.project.email = arr[0]
          //  obj.chaoemail=arr[1]
          }
          console.log(this.project)
          if (this.project.lastYearSales) {
            this.project.lastYearSales = this.project.lastYearSales / 100
          }
          if (this.project.beforLastYearSales) {
            this.project.beforLastYearSales = this.project.beforLastYearSales / 100
          }
          if (this.project.thisYearSales) {
            this.project.thisYearSales = this.project.thisYearSales / 100
          }
          if (this.project.perCapitaSales) {
            this.project.perCapitaSales = this.project.perCapitaSales / 10000000
          }
          if (this.project.top3Buys) {
            this.project.top3Buys = this.project.top3Buys / 100
          }
          if (this.project.top2Buys) {
            this.project.top2Buys = this.project.top2Buys / 100
          }
          if (this.project.top1Buys) {
            this.project.top1Buys = this.project.top1Buys / 100
          }
          if (this.project.id) {
            this.getfiles(1)
            this.getfiles(2)
            this.getfiles(3)
            this.getfiles(4)
            this.getfiles(5)
            this.getfiles(6)
            this.getfiles(7)
          }
        }
        this.updateListSuplier(this.project)
      })
    },
    handleShowProjectFile1(type) {
      var row = {}
      if (type == 1) {
        row = this.file1
      } else if (type == 2) {
        row = this.file2
      } else if (type == 3) {
        row = this.file3
      } else if (type == 4) {
        row = this.file4
      } else if (type == 5) {
        row = this.file5
      } else if (type == 6) {
        row = this.file6
      } else if (type == 7) {
        row = this.file7
      }
      if (!row.id) {
        this.$message({
          type: 'warning',
          message: '此处还未上传文件'
        })
        return false
      }
      download(row.id).then(res => {
        const content = res
        const blob = new Blob([content]) // 构造一个blob对象来处理数据
        const fileName = row.fileName // 导出文件名
        if ('download' in document.createElement('a')) { // 支持a标签download的浏览器
          const link = document.createElement('a') // 创建a标签
          link.download = fileName // a标签添加属性
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          document.body.appendChild(link)
          link.click() // 执行下载
          URL.revokeObjectURL(link.href) // 释放url
          document.body.removeChild(link) // 释放标签
        } else { // 其他浏览器
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    turnsubmit: function(type) {
      var text = ''
      if (type == 1) {
        text = '确认通过?'
      } else {
        text = '确认驳回?'
      }
      var text1 = ''
      if (type == 1) {
        text1 = '已通过'
      } else {
        text1 = '已驳回'
      }
      this.$confirm(text, '警告', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const obj = {

          checkResult: type,

          id: this.shenpiId
        }
        this.fullscreenLoading = true
        check(obj).then((res) => {
          if (res.isSuccess) {
            this.$message({
              type: 'success',
              message: text1
            })
            // var obj1 = {
            //   functionMenu: this.urlMenu,
            //   id: this.project.id,
            //   functionType:1
            // }
            //  console.log('2112', this.project1)
            //  if(this.project1.estimateSpend>=300){
            //   obj1.functionType=2
            // }

            this.checklist()
            this.newsupplierList()
            this.getInfor(this.project1)
          } else {
            this.$message({
              type: 'warning',
              message: res.message
            })
          }
          this.fullscreenLoading = false
        }).catch((err) => {
          this.fullscreenLoading = false
        })
      }).catch(err => console.error(err))
    },
    isshow() {
      this.shenpiId = ''
      this.showpan = false
      if (this.whatcheck == 1) {
        for (var i = 0; i < this.tableData2.length; i++) {
          if (this.tableData2[i].checkResult == 0) {
            console.log(1)
            if (this.tableData2[i].userId == this.$store.state.user.userId && this.tableData2[i].noCheck !== 1) {
              console.log(2)
              this.shenpiId = this.tableData2[i].id
              console.log('2222344', this.shenpiId)
              this.showpan = true

              return false
            } else {
              return false
            }
          }
        }
      } else {
        for (var i = 0; i < this.tableData2.length; i++) {
          if (this.tableData2[i].checkResult == 0) {
            if (this.tableData2[i].name == this.position) {
              console.log(21)
              this.shenpiId = this.tableData2[i].id
              console.log('2222344', this.shenpiId)
              this.showpan = true

              return false
            } else {
              return false
            }
          }
        }
      }
    },
    newsupplierList() {
      list(this.query).then((res) => {
        this.tableData = res.data.items
        this.total = res.data.items.length
        console.log(res)
      })
    },
    checklist() {
      const obj = {
        functionMenu: this.urlMenu,
        functionId: this.getShenPi,
        functionType: 1
      }
      console.log(this.project1)
      console.log(this.project)
      if (this.project1.estimateSpend >= 300) {
        obj.functionType = 2
      }
      getcheck(obj).then((res) => {
        this.tableData2 = res.data
        if (this.whatcheck == 1) {
          this.txtName = '姓名'
          console.log(11)
          this.isshow()
        } else {
          this.txtName = '岗位名称'
          this.getorg()
        }
      })
    },

    getwhart() {
      const obj = {
        functionMenu: this.urlMenu
      }
      getwhartCheck(obj).then((res) => {
        this.whatcheck = res.data.whatCheck
        this.checklist(this.project)
      })
    },
    getorg() {
      const obj = {
        username: this.$store.state.user.name
      }
      getorgByUsername(obj).then((res) => {
        this.position = res.data[0].titleName
        this.isshow()
      })
    },

    ProblemsState(row, $index) {
      this.tableData2 = []
      // this.dialogType = 'edit'
      // console.log(row)
      // this.text = '编辑'
      // this.dialogVisible = true
      this.getShenPi = row.id
      // this.pagesize = 5
      this.showtxt = true
      // this.project = deepClone(row)
      // this.project1 = deepClone(row)

      // if(this.project.email.indexOf(',')!==-1){
      //     let arr= this.project.email.split(',')
      //     this.project.email=arr[0]
      //     //  obj.chaoemail=arr[1]
      // }
      // this.getInfor(row)
      this.getwhart()
      //  this.getcompanyList()
      // this.checked=row.companyId.split(",")
      //    this.formDate11=''
      //  this.formDate11=row.establishmentTime
      // this.getfileOnSupplierFun(11)
      //  this.getfileOnSupplierFun(12)
      //  this.getSupplierCategoryFun()
    }
    // search() {
    //   this.query.pageIndex = 1
    //   this.newsupplierList()
    // },
    // handleAdd() {
    //   this.project = Object.assign({}, project)
    //   this.dialogType = 'new'
    //   this.dialogVisible = true
    // },
  }
}
</script>
<style>
.downfile:hover {
  text-decoration: underline;
  color: blue;
}

.delbtn {
  width: 20px;
  height: 20px;
  margin-left: 10px;
}
</style>

